package com.warrant.module.rpa.finance.response;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eos.system.annotation.Bizlet;

/**
 * 响应结果生成工具
 */
@Bizlet
public class ResultGenerator {

	private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";

	@Bizlet
	public static Result genSuccessResult() {
		return new Result().setCode(ResultCode.SUCCESS).setMsg(DEFAULT_SUCCESS_MESSAGE);
	}

	@Bizlet
	public static <T> Result<T> genSuccessResult(T data) {
		return new Result().setCode(ResultCode.SUCCESS).setMsg(DEFAULT_SUCCESS_MESSAGE).setData(data);
	}

	@Bizlet
	public static <T> Result<T> genSuccessListResult(List<T> list, Integer totalRecord) {
		Map data = new HashMap();
		data.put("list", list);
		data.put("total", totalRecord);
		return new Result().setCode(ResultCode.SUCCESS).setMsg(DEFAULT_SUCCESS_MESSAGE).setData(data);
	}

	@Bizlet
	public static Result genFailResult(String message) {
		return new Result().setCode(ResultCode.FAIL).setMsg(message).setData("");
	}

	@Bizlet
	public static <T> Result<T> genFailResult(String message, T data) {
		return new Result().setCode(ResultCode.FAIL).setMsg(message).setData(data);
	}

	/**
	 * 服务器异常数据处理
	 * 
	 * @param e
	 *            异常信息
	 * @return
	 */
	@Bizlet
	public static Result genErrorResult(Object e) {
		if (e instanceof Exception) {
			return new Result().setCode(ResultCode.INTERNAL_SERVER_ERROR).setMsg("系统异常:" + ((Exception) e).toString()).setData(e);
		}
		return new Result().setCode(ResultCode.INTERNAL_SERVER_ERROR).setMsg("系统异常").setData(e);
	}
}
